uniform extern float4x4 gWVP;
uniform extern float2   gTexScale;
uniform extern float3   gDirToSunW; // Assumed to be unit length.

struct OutputVS
{
    float4 posH         : POSITION0;
    float  shade        : TEXCOORD0;
    float2 tiledTexC    : TEXCOORD1; 
    float2 nonTiledTexC : TEXCOORD2;
};


OutputVS Terrain_MultiTexVS(float4 posL         : POSITION0, 
                            float3 normalL      : NORMAL0,  // Assumed to be unit length
                            float2 tiledTexC    : TEXCOORD0,
                            float2 nonTiledTexC : TEXCOORD1)
{
OutputVS outVS = (OutputVS)0;


// Project position homogeneous clip space.
    outVS.posH  = mul(posL, gWVP); 
    
    // Do basic diffuse lighting calculating to compute vertex shade.
    outVS.shade = max(0.0f, dot(normalL, gDirToSunW));
    
    // Scale tiled tex-coords as specified by the application.
    outVS.tiledTexC.x  = tiledTexC.x * gTexScale.x;
    outVS.tiledTexC.y  = tiledTexC.y * gTexScale.y;
    
    // Forward non-tiled tex-coords to pixel shader.
    outVS.nonTiledTexC = nonTiledTexC;
    
  
    return outVS;
}


sampler  gLayerMap0;
sampler  gLayerMap1;
sampler  gLayerMap2;
sampler  gBlendMap;

float4 Terrain_MultiTexPS(float  shade        : TEXCOORD0,
                          float2 tiledTexC    : TEXCOORD1,
      float2 nonTiledTexC : TEXCOORD2) : COLOR
{
// Layer maps are tiled
    float3 c0 = tex2D(gLayerMap0, tiledTexC);
    float3 c1 = tex2D(gLayerMap1, tiledTexC);
    float3 c2 = tex2D(gLayerMap2, tiledTexC);

    // Blendmap is not tiled.
    float3 B = tex2D(gBlendMap, nonTiledTexC);

// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
    float totalInverse = 1.0f / (B.r + B.g + B.b);
    
    c0 *= B.r * totalInverse;
    c1 *= B.g * totalInverse;
    c2 *= B.b * totalInverse;
    
    float3 final = (c0 + c1 + c2) * shade;

    return float4(final, 1.0f);
} 
 